// 用户数据模块
import api from '@/common/request/index'
import store from '@/common/store'
import router from '@/common/router.js'
import tools from '@/common/utils/tools'

import {
	USER_INFO,
	LOGIN_TIP,
	ORDER_NUMBER,
	OUT_LOGIN,
	// #ifdef MP-WEIXIN
	FORCE_OAUTH,
	// #endif
} from '../types.js'
const state = {
	userInfo: uni.getStorageSync('userInfo') ? uni.getStorageSync('userInfo') : {},
	showLoginTip: false,
	orderNum: {},
	// #ifdef MP-WEIXIN
	forceOauth: false,
	// #endif
	messageIds: {}, //小程序订阅消息模板ids

}

const actions = {
	//设置token并返回上次页面
	setTokenAndBack({
		commit
	}, token) {
		uni.setStorageSync('token', token);
		store.dispatch('getUserInfo');
		let fromLogin = uni.getStorageSync('fromLogin');
		if (fromLogin) {
			tools.routerTo(fromLogin.path, fromLogin.query, true);
			uni.removeStorageSync('fromLogin')
		} else {
			//默认跳转首页S
			router.replaceAll('/pages/index/index')
		}
	},

	//登录
	Login({commit}, userInfo) {
		return new Promise((resolve, reject) => {
			api('user.login', userInfo).then(res => {
				if (res.code == 200) {
					store.dispatch('setTokenAndBack', res.data.token)
					uni.setStorageSync('tokenHead', res.data.tokenHead)
				} else if (res.code == 404) {
					uni.showToast({ title: res.message, icon: 'none'})
				}
			}).catch(error => {
				reject(error)
			})
		})
	},

	//手机号登录
	Mobile({commit}, userInfo) {
		return new Promise((resolve, reject) => {
			api('user.mobile', userInfo).then(res => {
				if (res.code == 200) {
					store.dispatch('setTokenAndBack', res.data.token)
					uni.setStorageSync('tokenHead', res.data.tokenHead)
				}
			}).catch(error => {
				reject(error)
			})
		})
	},

	// 获取用户信息
	getUserInfo({ commit }) {
		return new Promise((resolve, reject) => {
			api('user.info').then(res => {
				// store.dispatch('getCartList')
				commit('LOGIN_TIP', false);
				commit('USER_INFO', res.data);
				uni.setStorageSync('userInfo', res.data);
				// store.dispatch('getOrderNum');
				resolve(res)
			}).catch(e => {
				reject(e)
			})
		})
	},

	//  退出登录
    LogOut({ commit, state }) {
        return new Promise((resolve, reject) => {
            api('user.logout').then(() => {
                uni.removeStorageSync('token');
                uni.removeStorageSync('userInfo');
                uni.removeStorageSync('cartNum');
                commit('USER_INFO', {});
                commit('ORDER_NUMBER', {});
                router.replaceAll('/pages/index/index')
                resolve()
            }).catch(error => {
                reject(error)
            })
        })
    },
	// 订单信息
	getOrderNum({commit}) {
		return new Promise((resolve, reject) => {
			api('order.page').then(res => {
				commit('ORDER_NUMBER', res.data.list);
				resolve(res)
			}).catch(e => {
				reject(e)
			})
		})
	},
}

const mutations = {
	[USER_INFO](state, data) {
		state.userInfo = data
	},
	[LOGIN_TIP](state, data) {
		state.showLoginTip = data
	},
	[ORDER_NUMBER](state, data) {
		state.orderNum = data
	},
	// #ifdef MP-WEIXIN
	[FORCE_OAUTH](state, data) {
		state.forceOauth = data
		data ? uni.hideTabBar() : uni.showTabBar();
	},
	// #endif

	[OUT_LOGIN](state, data) {
		uni.removeStorageSync('token');
		uni.removeStorageSync('userInfo');
		uni.removeStorageSync('cartNum');
		store.commit('USER_INFO', {});
		// store.commit('CART_LIST', []);
		// store.commit('CART_NUM');
		store.commit('ORDER_NUMBER', {});
	},

}

const getters = {

}

export default {
	state,
	mutations,
	actions,
	getters
}
